/* Copyright (C) 2007 Notyet

这是algerbra库的一部分。

algerbra是一个基于C开发的库，旨在解决代数式化简与解简单方程的问题。

联系方式：
E-Mail:nt1900@gmail.com  */
#define _DEBUG__
#include "libalgebra.h"
#include "malloc.h"
#ifdef _DEBUG__
#include <stdio.h>
#endif
//---------------------------init
DLLIMPORT extern void alg_formula_init()
{

}

//---------------------------create&destroy
DLLIMPORT extern FORMULA* alg_create_formula(char attr)
{
	FORMULA* fml;
	fml=(FORMULA*)malloc(sizeof(FORMULA));
	if(NULL==fml)return NULL;
	switch(alg_check_formula_attr(attr))
	{
		case FORMULA_ATTR_TERMS:		fml->data.terms.head=NULL;
							fml->data.terms.count=0;
							break;
		case FORMULA_ATTR_FRACTION:	fml->data.fraction.numerator=0;
							fml->data.fraction.denominator=1;
							break;
		case FORMULA_ATTR_RADICAL:	fml->data.radical.radicand=0;
							fml->data.radical.root=2;
							break;
		case FORMULA_ATTR_LETTER:	fml->data.letter.letter_id=0;
							fml->data.letter.power=0;
							break;
		case FORMULA_ATTR_FUNCTION:	fml->data.function.func_id=0;
							fml->data.function.arguments=NULL;
							break;
	}
	fml->attr=attr;
	return fml;
}

DLLIMPORT extern int alg_destroy_formula(FORMULA* fml)
{
	if(NULL==fml)return 0;
	if((FORMULA_ATTR_TERMS==alg_check_formula_attr(fml->attr))&&(NULL!=fml->data.terms.head))
	{
		if(!alg_destroy_terms(fml->data.terms.head))return 0;
	}
	if(NULL!=fml->coefficient)
	{
		if(!alg_destroy_formula(fml->coefficient))return 0;
	}
	free(fml);
	#ifdef _DEBUG__
	printf(" ff");
	#endif
	return 1;
}

DLLIMPORT extern CHAIN* alg_create_term(FORMULA* term)
{
	CHAIN* chn;
	chn=(CHAIN*)malloc(sizeof(CHAIN));
	if(NULL==chn)return NULL;
	chn->next=NULL;
	chn->term=term;
	return chn;
}

DLLIMPORT extern int alg_destroy_term(CHAIN* chn)
{
	if(NULL==chn)return 0;
	free(chn);
	#ifdef _DEBUG__
	printf(" ft");
	#endif
	return 1;
}

DLLIMPORT extern int alg_destroy_terms(CHAIN* head)
{
	if(NULL==head)return 0;
	if(NULL!=head->next)
	{
		if(!alg_destroy_terms(head->next))return 0;
	}
	if(NULL!=head->term)
	{
		if(!alg_destroy_formula(head->term))return 0;
	}
	if(!alg_destroy_term(head))return 0;
	return 1;
}

//---------------------------compute
DLLIMPORT extern int alg_add(FORMULA* dsc,FORMULA* src)
{
	switch(alg_check_formula_attr(dsc->attr))
	{
		case FORMULA_ATTR_TERMS:
			CHAIN* tmp;
			tmp=alg_create_term(src)
			if(tmp==NULL)return 0;
			tmp->next=dsc->head;
			dsc->head=tmp;
			break;
		case FORMULA_ATTR_FRACTION:
		case FORMULA_ATTR_RADICAL:
		case FORMULA_ATTR_LETTER:
		case FORMULA_ATTR_FUNCTION:
			FORMULA* tmp;
			tmp=alg_create_formula(
	}
	return 1;
}
DLLIMPORT extern int alg_mul(FORMULA* dsc,FORMULA* src){return 0;}
DLLIMPORT extern int alg_neg(FORMULA* obj)
{
	if((obj->attr)>>7)(obj->attr)-=FORMULA_ATTR_NEG;
	else (obj->attr)|=FORMULA_ATTR_NEG;
	return 1;
}
DLLIMPORT extern int alg_rec(FORMULA* obj)
{
	if((obj->attr)>>6)(obj->attr)-=FORMULA_ATTR_REC;
	else (obj->attr)|=FORMULA_ATTR_REC;
	return 1;
}
DLLIMPORT extern int alg_format(FORMULA* obj){return 0;}
//---------------------------simplification
DLLIMPORT extern int alg_simplification(FORMULA* obj){return 0;}
DLLIMPORT extern int alg_simp_redution(){return 0;}
DLLIMPORT extern int alg_simp_rational(){return 0;}
DLLIMPORT extern int alg_combine_like_terms(FORMULA* obj){return 0;}





/*
	switch(alg_check_formula_attr(attr))
	{
		case FORMULA_ATTR_TERMS:
		case FORMULA_ATTR_FRACTION:
		case FORMULA_ATTR_RADICAL:
		case FORMULA_ATTR_LETTER:
		case FORMULA_ATTR_FUNCTION:
	}
*/

/*
	switch(alg_check_formula_attr(attr))
	{
		case FORMULA_ATTR_TERMS:		fml->data.terms.head=NULL;
							fml->data.terms.count=0;
							break;
		case FORMULA_ATTR_FRACTION:	fml->data.fraction.numerator=0;
							fml->data.fraction.denominator=1;
							break;
		case FORMULA_ATTR_RADICAL:	fml->data.radical.radicand=0;
							fml->data.radical.root=2;
							break;
		case FORMULA_ATTR_LETTER:	fml->data.letter.letter_id=0;
							fml->data.letter.power=NULL;
							break;
		case FORMULA_ATTR_FUNCTION:	fml->data.function.func_id=0;
							fml->data.function.arguments=NULL;
							break;
	}
*/
